package com.bbz.core.concurrency.work1;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
/**
 * 创建线程的几种方式
 * @author binbin.a.zhang
 *
 */
public class CreateThreadDemo {
	public static void main(String args[]) {
		// 继承Thead
		Thread thread = new Thread() {
			@Override
			public void run() {
				System.out.println("继承Thread");
				super.run();
			}
		};
		thread.start();
		// 实现Runable接口
		Thread thread1 = new Thread(new Runnable() {

			@Override
			public void run() {
				System.out.println("实现Runable接口");
			}
		});
		thread1.start();
		// 实现Callable接口
		ExecutorService executorService =  Executors.newCachedThreadPool();
		Future<String> future = executorService.submit(new Callable() {

			@Override
			public String call() throws Exception {
				return "实现Callable接口";
			}
		});
		try {
			String result = future.get();
			System.out.println(result);
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}

	}

}
